package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.KeyEvent
import android.widget.EditText
import com.glory.cqaospda.R
import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.viewmodel.ErrorModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.glorysoft.hlgdpda.utils.PopWindowsUtils
import com.google.gson.JsonObject
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager
import kotlinx.android.synthetic.main.activity_error.*
import kotlinx.android.synthetic.main.activity_title.*
import java.lang.Exception

class ErrorActivity : BaseActivity<ErrorModel>() {

    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@ErrorActivity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }

    private var codePop: PopWindowsUtils.GeneralPop? = null

    override fun initData() {

        viewModel.defUI.callObserve.observe(this@ErrorActivity) {
            if (!Constant.okgoError) {
                when (it.type) {
                    1 -> {
                        viewModel.defUI.toastEvent.value = ToastMessageBean("提交成功", true)
                        intent.setClass(this, HomeActivity::class.java)
                        startActivity(intent)
                        finish()
                    }
                }
            }else{
                Constant.okgoError = false
            }
        }

        tvBaseSubmit.setOnClickListener {
            if (Constant.isFastClick()) {
                if (etEqpId.text.toString() == "") {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请扫描设备号", false)
                    return@setOnClickListener
                }
                if (etLotId.text.toString() == "") {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请扫描批次号", false)
                    return@setOnClickListener
                }
                if (tvType.text.toString() == "") {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("请选择撤料原因", false)
                    return@setOnClickListener
                }

                val map = JsonObject()
                map.addProperty("EquipmentName", etEqpId.text.toString())
                map.addProperty("LotID", etLotId.text.toString())
                map.addProperty("ReasonCode", tvType.text.toString())
                map.addProperty("Comment", etComment.text.toString())

                viewModel.defaultAbnormalTrack(map)
            }
        }

    }

    override fun initView(savedInstanceState: Bundle?) {
        tvBaseSubmit.text = "确认"
        tvTitle.text = "异常结束"

        tvUserName.text = Constant.UserName
        FreeApi_ActivityManager.getActivityManager().addActivity(this)

        val list: ArrayList<String> = ArrayList()
//        list.add("产品测试异常")
//        list.add("系统运行异常")
//        list.add("改机换料")
//        list.add("其他")
        list.add("Product issue")
        list.add("EQP issue")
        list.add("MFG change")
        list.add("Other")
        flPositionList.setOnClickListener {
            if (codePop == null) {
                codePop = PopWindowsUtils.getOneChoosePop(this, flType.width, list,
                    ivTypeArrow, object : PopWindowsUtils.PopTextAndPositionCallback {
                        override fun onSuccess(string: String, potion: Int) {
                            tvType.text = string
                        }
                    }
                )
            }

            if (codePop!!.isShowing) {
                codePop?.dismiss()
            } else {
                PopWindowsUtils.setArrowShow(ivTypeArrow)
                codePop?.showAsDropDown(flType, 0, 0)
            }
        }

        val instance = MyApp.instance
        instance!!.handler = handler
    }

    override fun layoutId(): Int {
        return R.layout.activity_error
    }

    @SuppressLint("SetTextI18n")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 1) {
            when (resultCode) {
                108 -> {
                    finish()
                }
            }
        }
    }

    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            intent.putExtra("type", "KeyEvent")
            intent.setClass(this, SkipDialogActivity::class.java)
            startActivityForResult(intent, 1)
            return false
        }
        return super.onKeyUp(keyCode, event)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        try {
            val view = window.decorView.findFocus() as EditText
            if (keyCode == 520) {
                when (view) {
                    etEqpId -> {
                        etEqpId.setText("")
                    }
                    etLotId -> {
                        etLotId.setText("")
                    }
                }
            }
        } catch (e: Exception){
            println(e.message)
        }
        return super.onKeyDown(keyCode, event)
    }
}